package com.ny.service.impl;

import com.ny.Util.Passwordutil;
import com.ny.entity.User;
import com.ny.mapper.USerMapper;
import com.ny.service.EmailService;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServicelmpl implements UserService {
    @Resource
    private USerMapper userMapper;

    // 确保所有接口方法都有实现

    @Override
    public List<User> findAllUser() {
        return userMapper.findAllUser();
    }

    @Override
    public User login(String username, String password) {
        return findUser(username, password);
    }

    @Override
    public User findUserByUsername(String username) {
        return userMapper.findUserByUsername(username);
    }

    @Override
    public int registerUser(User user) {
        // 对密码进行MD5加密
        user.setPassword(Passwordutil.md5(user.getPassword()));
        return userMapper.registerUser(user);
    }

    @Override
    public void sendEmailCode(String email) {
        // 实现发送邮件验证码逻辑
        // 这里需要添加具体的实现代码
        System.out.println("发送邮件验证码到: " + email);
        // TODO: 实现邮件发送逻辑
    }

    @Override
    public User findUserByEmail(String email) {
        return userMapper.findUserByEmail(email);
    }

    @Override
    public int updatePasswordByEmail(String email, String password) {
        return userMapper.updatePasswordByEmail(email, Passwordutil.md5(password));
    }

    @Override
    public boolean checkCode(String email, String code) {
        // 实现验证码检查逻辑
        // 这里需要添加具体的实现代码
        System.out.println("检查验证码: email=" + email + ", code=" + code);
        // TODO: 实现验证码验证逻辑
        return false; // 临时返回false
    }

    @Override
    public int updateUserInfo(User user) {
        return userMapper.updateUserInfo(user);
    }

    @Override
    public int updatePassword(String username, String newPassword) {
        return userMapper.updatePassword(username, Passwordutil.md5(newPassword));
    }

    @Override
    public User findUser(String username, String password) {
        System.out.println("=== 详细调试信息 ===");
        System.out.println("接收到的用户名: '" + username + "'");
        System.out.println("接收到的密码: '" + password + "'");
        System.out.println("用户名长度: " + username.length());
        System.out.println("密码长度: " + password.length());

        // 检查是否有逗号分隔的重复值
        if (username.contains(",")) {
            System.out.println("警告：用户名包含逗号，可能有重复参数！");
            // 取第一个值
            username = username.split(",")[0];
            System.out.println("修正后的用户名: '" + username + "'");
        }
        if (password.contains(",")) {
            System.out.println("警告：密码包含逗号，可能有重复参数！");
            // 取第一个值
            password = password.split(",")[0];
            System.out.println("修正后的密码: '" + password + "'");
        }

        String md5Password = Passwordutil.md5(password);
        System.out.println("MD5加密后: " + md5Password);

        // 先尝试MD5加密查询
        User user = userMapper.findUser(username, md5Password);
        System.out.println("MD5查询结果: " + user);

        if (user != null) {
            return user;
        }

        // 如果MD5查询失败，尝试明文密码查询
        System.out.println("MD5查询失败，尝试明文查询");
        user = userMapper.findUser(username, password);
        System.out.println("明文查询结果: " + user);

        return user;
    }
}